package com.pzj.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzj.dormitory.entity.Building;
import com.pzj.dormitory.entity.Dormitory;
import com.pzj.dormitory.entity.Student;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.mapper.BuildingMapper;
import com.pzj.dormitory.mapper.DormitoryMapper;
import com.pzj.dormitory.mapper.StudentMapper;
import com.pzj.dormitory.service.DormitoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzj.dormitory.vo.DormitoryVO;
import com.pzj.dormitory.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@Service
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements DormitoryService {
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public PageVO list(Integer pageNum, Integer pageSize) {
        Page<Dormitory> dormitoryPage = new Page<>(pageNum, pageSize);
        Page<Dormitory> page = dormitoryMapper.selectPage(dormitoryPage, null);
        ArrayList<DormitoryVO> dormitoryVOS = new ArrayList<>();
        for (Dormitory dormitory : page.getRecords()){
            DormitoryVO dormitoryVO = new DormitoryVO();
            BeanUtils.copyProperties(dormitory, dormitoryVO);
            Building building = buildingMapper.selectById(dormitory.getBuildingId());
            dormitoryVO.setBuildingName(building.getName());
            dormitoryVOS.add(dormitoryVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(dormitoryVOS);
        pageVO.setTotal(page.getTotal());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Dormitory> dormitoryPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Dormitory> page = null;
        if(searchForm.getValue().equals("")){
            page = dormitoryMapper.selectPage(dormitoryPage, null);
        }else {
            QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),searchForm.getValue());
            page = dormitoryMapper.selectPage(dormitoryPage, queryWrapper);
        }
        ArrayList<DormitoryVO> dormitoryVOS = new ArrayList<>();
        List<Dormitory> dormitoryList = page.getRecords();
        for (Dormitory dormitory : dormitoryList) {
            DormitoryVO dormitoryVO = new DormitoryVO();
            BeanUtils.copyProperties(dormitory, dormitoryVO);
            Building building = buildingMapper.selectById(dormitory.getBuildingId());
            dormitoryVO.setBuildingName(building.getName());
            dormitoryVOS.add(dormitoryVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(dormitoryVOS);
        pageVO.setTotal(page.getTotal());
        return pageVO;
    }

    @Override
    public boolean deleteById(Integer id) {
        // 查找宿舍中所有学生
        // 分配学生到空闲宿舍
        // 删除宿舍
        List<Student> studentList = studentMapper.selectList(new QueryWrapper<Student>().eq("dormitory_id", id));
        for (Student student : studentList){
            Integer availableDorId = dormitoryMapper.findAvailableDorId();
            student.setDormitoryId(availableDorId);
            try {
                // 更新学生宿舍
                studentMapper.updateById(student);
                // 床位-1
                dormitoryMapper.subAvailableById(availableDorId);
            } catch (Exception e) {
                return false;
            }
        }
        // 删除宿舍
        int i = dormitoryMapper.deleteById(id);
        if(i!=1)return false;
        return true;
    }
}


